package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.entities.construct.ResultReturn;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * 支付模块
 * @author zxl
 *
 */
@RestController
@RequestMapping("payment")
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/create")
    public CommonResult create( @RequestBody Payment payment ){
        log.info("服务端接受到的数据"+ payment.getSerial());
        int i = paymentService.create(payment);
        if ( i > 0 ) {
            return new CommonResult(ResultReturn.success,"插入成功，serverPort:" + serverPort ,i);
        } else {
            return new CommonResult(ResultReturn.error,"插入失败，serverPort:" + serverPort , null);
        }
    }

    @GetMapping("/getPaymentById/{id}")
    public CommonResult getPaymentById(@PathVariable("id") long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询结果集:"+ payment);
        if ( payment != null ) {
            return new CommonResult(ResultReturn.success,"查询成功，serverPort:" + serverPort ,payment);
        } else {
            return new CommonResult(ResultReturn.error,"查询失败，serverPort:" + serverPort , null);
        }
    }

    /**
     * 返回当前端口号
     * @return
     */
    @GetMapping("/lb")
    public String getPaymentLB(){
        return serverPort;
    }
}